package io.imgselector;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import java.io.File;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.embedding.engine.plugins.FlutterPlugin;

import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.PluginRegistry;
import takephoto.activity.TakePicActivity;
import record.RecordVideoActivity;
import utils.BitmapUtils;
import utils.WaterUtils;

import io.flutter.plugin.common.MethodCall;

public class FlutterNativeImgPlugin implements MethodCallHandler, FlutterPlugin, PluginRegistry.ActivityResultListener {
    public static String CHANNEL = "com.ehs2.photo_select"; // 分析1
    public static int REQUEST_CODE = -3324;
    private Activity activity;
    private MethodChannel channel;

    public static MethodChannel.Result result;
    public static FlutterNativeImgPlugin imgPlugin;
    public FlutterNativeImgPlugin(Activity activity12) {
        this.activity =activity12;
        imgPlugin = this;
    }

    public static void registerWith(FlutterEngine flutterEngine, Activity activity12) {
        MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL);
        FlutterNativeImgPlugin instance = new FlutterNativeImgPlugin(activity12);
        channel.setMethodCallHandler(instance);
//        instance.addActivityResultListener(instance);
    }

    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        Log.e("tag", activity.getLocalClassName());
        this.result = result;
        try {

            if (methodCall.method.equals("takePhoto")) {
                Log.e("111111111111","222222"+FlutterNativeImgPlugin.this);
                String count11=methodCall.argument("count");
                int count =Integer.parseInt(count11) ;
                String adress=methodCall.argument("adress");
                Intent intent=new Intent(activity, TActivity.class);
                intent.putExtra("count",count);
                intent.putExtra("type","0");
                intent.putExtra("adress",adress);
                activity.startActivityForResult(intent, 12030);
            }else if (methodCall.method.equals("selectPic")){
                Log.e("111111111111","33333333"+FlutterNativeImgPlugin.this);
                String count11=methodCall.argument("count");
                int count =Integer.parseInt(count11) ;
                String adress=methodCall.argument("adress");
                Intent intent=new Intent(activity, TActivity.class);
                intent.putExtra("type","1");
                intent.putExtra("count",count);
                intent.putExtra("adress",adress);
                activity.startActivityForResult(intent, 12030);
            }else if (methodCall.method.equals("selectVideo")){
                String count11=methodCall.argument("count");
                int count =Integer.parseInt(count11) ;
                String adress=methodCall.argument("adress");
                Intent intent=new Intent(activity, RecordVideoActivity.class);
                intent.putExtra("type","1");
                intent.putExtra("count",count);
                intent.putExtra("adress",adress);
                activity.startActivityForResult(intent, 12030);

            }else if (methodCall.method.equals("addWatermark")){


                String adress=methodCall.argument("adress");
                String filePath=methodCall.argument("filePath");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                WaterUtils.addWatermarkBitmap(BitmapUtils.getBitmapByFile(new File(filePath)),df.format(new Date()),adress,filePath);
                result.success(filePath);

            }else {
                result.notImplemented();
            }
        } catch (Exception e){
            e.printStackTrace();
            result.success("");
        }
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("这是回调的方法里");
        if (requestCode==12030||resultCode==10123){
            if (data!=null&&data.getStringArrayListExtra("file")!=null&&result!=null){
                Log.e("afafafafg",data.getStringArrayListExtra("file").toString());
//            Toast.makeText(activity, intent.getStringArrayListExtra("file")+"", Toast.LENGTH_SHORT).show();
                result.success(data.getStringArrayListExtra("file"));
                result=null;
            }

            return true;
        }
        return false;
    }


    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        activity=binding.getActivity();
        binding.addActivityResultListener(this);
    }

    @Override
    public void  onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
    MethodChannel channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL);

    channel.setMethodCallHandler(this);
}


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

    }
}


